import wepy from 'wepy'

export default class extends wepy.mixin {
  data = {
    // 分类的数据列表
    cateList: [],
    // 控制选中第几项
    activeKey: 0,
    // 当前屏幕可用的高度
    wh: 0,
    // 二级分类数据
    secondData: []
  }

  // 获取分类的数据列表的方法
  async getCateList () {
    const {data: res} = await wepy.get('/categories')

    console.log(res)

    // 判断返回的数据是否成功
    if (res.meta.status !== 200) {
      return wepy.baseToast()
    }

    this.cateList = res.message
    this.secondData = res.message[0].children
    console.log(this.secondData)
    this.$apply()
  }

  async getWindowHeight () {
    const res = await wepy.getSystemInfo()

    if (res.errMsg === 'getSystemInfo:ok') {
      this.wh = res.windowHeight
      this.$apply()
    }
  }

  onLoad() {
    this.getCateList()
    this.getWindowHeight()
  }

  methods = {
    onChange (e) {
      // 通过 e.detail 获取当前分类的索引
      console.log(e.detail)

      this.secondData = this.cateList[e.detail].children
      console.log(this.secondData)
    },

    goGoodList (cid) {
      wepy.navigateTo({
        url: '/pages/goods_list?cid=' + cid
      })
    }
  }
}
